<?php

	/**
	 * Bike_Js_Ext_form_field_Field
	 *
	 * Any options passed will be used as setting of ExtJs config object.
	 *
	 *
	 * @category   Bike
	 * @package    Bike_Js
	 * @subpackage Ext
	 * @copyright  Copyright (c) 2012 DF-Studio. (http://www.df-studio.net)
	 * @version    1
	 */

	/**
	 * @see Bike_Js_Ext_XType
	 */
	require_once 'Bike/Js/Ext/XType.php';


	class Bike_Js_Ext_form_field_Field extends Bike_Js_Ext_XType
	{

		/**
		 * Get default settings for ExtJs config object
		 *
		 * @return array
		 */

		protected function initDefaultSettings()
		{
			parent::initDefaultSettings();

			$js = $this->js();
			$this->setSettings(array(
				'allowBlank' => False,
				'enableKeyEvents' => True,
				'msgTarget' => 'side',
				'description' => null,											// description of field
				'descriptionIconCls' => 'x-form-field-description-icon',		// class name of description icon; default: x-form-field-description-icon
				'descriptionIconLabel' => null,									// label of description icon

				'required' => false,											// is field required
				'requiredIconCls' => 'x-form-field-required-icon',				// class name of description icon; default: x-form-field-required-icon
				'requiredIconLabel' => null,									// label of required icon

				'boxLabelAlign' => 'after',

				'listeners' => array(
					'render' => $this->getJsListenersRender(),
				),
			));
		}


		/**
		 * Get JS listeners render
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersRender()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->item)->_body(array(
						// apply description
						$this->getJsListenersRenderDescriptionHandler(),
						// apply required
						$this->getJsListenersRenderRequiredHandler(),
						// apply boxLabel
						$this->getJsListenersRenderBoxLabelHandler(),
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}


		/**
		 * Get description setting handler to construct description label of field
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersRenderDescriptionHandler()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__, $js->if($js->item->description)->_body(array(
					$js->_var($js->description)->_val($js->item->labelEl->createChild(array(
						'tag' => 'span',
						'class' => $js->item->descriptionIconCls,
						'data-qtip' => $js->item->description,
						'html' => $js->item->descriptionIconLabel,
					))),
				)));
			}
			return $this->getParamCache(__METHOD__);
		}

		/**
		 * Get required setting handler to construct required label of field
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersRenderRequiredHandler()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__, $js->if($js->item->required)->_body(array(
					$js->_var($js->required)->_val($js->item->labelEl->createChild(array(
						'tag' => 'span',
						'class' => $js->item->requiredIconCls,
						'data-qtip' => $this->translate('This field is required'),
						'html' => $js->item->requiredIconLabel,
					))),
				)));
			}
			return $this->getParamCache(__METHOD__);
		}

		/**
		 * Get boxLabel setting handler to construct boxLabel of any field
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersRenderBoxLabelHandler()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__, $js->if($js->item->boxLabel)->_body(array(
					$js->if($js->_not($js->item->boxLabelEl))->_body(array(
						$js->_var($js->bodyTabElId)->_val($js->item->id->_add('-bodyTabEl')),
						$js->_var($js->bodyCellId)->_val($js->item->id->_add('-bodyCell')),
						$js->_var($js->boxLabelCellId)->_val($js->item->id->_add('-boxLabelCell')),
						$js->_var($js->boxLabelElId)->_val($js->item->id->_add('-boxLabelEl')),
						$js->_var($js->items)->_val(array()),
						$js->if($js->item->boxLabelAlign->_eq('after'))->_body(array(
							$js->items->push(array(
								'tag' => 'td',
								'id' => $js->bodyCellId,
							)),
						)),
						$js->items->push(array(
							'tag' => 'td',
							'id' => $js->boxLabelCellId,
							'cn' => array(
								'tag' => 'label',
								'id' => $js->boxLabelElId,
								'for' => $js->item->id->_add('-inputEl'),
								'class' => $js->Ex('x-form-cd-label x-form-cb-label-')->_add($js->item->boxLabelAlign),
								'html' => $js->item->boxLabel,
							),
						)),
						$js->if($js->item->boxLabelAlign->_neq('after'))->_body(array(
							$js->items->push(array(
								'tag' => 'td',
								'id' => $js->bodyCellId,
							)),
						)),
						$js->item->bodyEl->createChild(array(
							'tag' => 'table',
							'id' => $js->bodyTabElId,
							'style' => '-moz-user-select: -moz-none; width: 100%; table-layout:fixed',
							'cn' => array(
								'tag' => 'tbody',
								'cn' => array(
									'tag' => 'tr',
									'cn' => $js->items,
								),
							),
						)),
						$js->item->boxLabelEl->_val($js->Ext->get($js->boxLabelElId)),
						$js->Ext->dom->Element->get($js->boxLabelCellId)->setWidth($js->item->boxLabelEl->getComputedWidth()->_add(5)),
						$js->while($js->item->bodyEl->first()->id->_neq($js->bodyTabElId))->_body(array(
							$js->item->bodyEl->first()->appendTo($js->bodyCellId),
						)),
					)),
				)));
			}
			return $this->getParamCache(__METHOD__);
		}

	}
